package game.poker.holdem.card;

import java.util.HashMap;
import java.util.Map;

public enum RankComparators implements RankComparator {
    ACE_HIGH {        
        {
        	int kingValue = rankValueMap.get(Rank.KING);
            int aceValue = kingValue + VALUE_STEP;
            rankValueMap.put(Rank.ACE, aceValue);
        }
    }, 
    ACE_LOW {
        {
        	int twoValue = rankValueMap.get(Rank.TWO);
            int aceValue = twoValue - VALUE_STEP;
            rankValueMap.put(Rank.ACE, aceValue);
        }
    };
    
    protected int VALUE_STEP;
    protected Map<Rank, Integer> rankValueMap;
    
    {
    	 VALUE_STEP = 1;
         Rank[] ranks = {
                 Rank.TWO, 
                 Rank.THREE, 
                 Rank.FOUR, 
                 Rank.FIVE, 
                 Rank.SIX, 
                 Rank.SEVEN, 
                 Rank.EIGHT, 
                 Rank.NINE, 
                 Rank.TEN, 
                 Rank.JACK, 
                 Rank.QUEEN, 
                 Rank.KING };            
         rankValueMap = new HashMap<Rank, Integer>(ranks.length + 1);            
         int value = 2;
         for(Rank rank : ranks) {
             rankValueMap.put(rank, value);
             value += VALUE_STEP;
         }
    }
  
    public Integer getValue(Rank rank) {
        return rankValueMap.get(rank);
    }
    
    @Override
    public int compare(Rank r1, Rank r2) {
        Integer v1 = rankValueMap.get(r1);
        Integer v2 = rankValueMap.get(r2);
        return v1.compareTo(v2);
    }
    
    public boolean isConsecutive(Rank low, Rank high) {
        int lowValue = rankValueMap.get(low);
        int highValue = rankValueMap.get(high);        
        return ((lowValue + VALUE_STEP) == highValue);
    }
}
